package com.example.controller;

import com.common.result.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.pojo.dto.QueryPageDTO;
import com.pojo.dto.SetmealDTO;
import com.pojo.entity.Setmeal;
import com.example.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author ccz
 * @since 2025-07-19
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;

    /**
     * 新增套餐
     */
    @PostMapping
    public Result save(@RequestBody Setmeal setmeal) {
        setmealService.save(setmeal);
        return Result.success("新增套餐成功");
    }

    /**
     * 分页查询套餐 - 只查询type=2的分类下的套餐，包含分类名称
     */
    @GetMapping("/page")
    public Result page(QueryPageDTO queryPageDTO) {
        PageHelper.startPage(queryPageDTO.getPageNum(), queryPageDTO.getPageSize());
        // 使用自定义查询获取包含分类名称的套餐数据
        List<SetmealDTO> list = setmealService.getSetmealWithCategory(queryPageDTO.getName());
        PageInfo<SetmealDTO> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /**
     * 查询所有套餐
     */
    @GetMapping
    public Result list() {
        List<Setmeal> list = setmealService.list();
        return Result.success(list);
    }

    /**
     * 根据分类ID查询套餐
     */
    @GetMapping("/category/{categoryId}")
    public Result getByCategoryId(@PathVariable Long categoryId) {
        // 使用传统方式查询，避免Lambda表达式
        List<Setmeal> list = setmealService.list();
        list = list.stream()
                .filter(setmeal -> setmeal.getCategoryId().equals(categoryId))
                .filter(setmeal -> setmeal.getStatus() == 1) // 只查询起售状态的套餐
                .collect(java.util.stream.Collectors.toList());
        return Result.success(list);
    }

    /**
     * 根据ID查询套餐
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        return Result.success(setmeal);
    }

    /**
     * 修改套餐信息
     */
    @PutMapping
    public Result update(@RequestBody Setmeal setmeal) {
        setmealService.updateById(setmeal);
        return Result.success("修改套餐成功");
    }

    /**
     * 删除套餐
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        setmealService.removeById(id);
        return Result.success("删除套餐成功");
    }

    /**
     * 批量删除套餐
     */
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        setmealService.removeByIds(ids);
        return Result.success("批量删除套餐成功");
    }

    /**
     * 更新套餐状态
     */
    @PutMapping("/status/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        // 先查询套餐是否存在
        Setmeal setmeal = setmealService.getById(id);
        if (setmeal == null) {
            return Result.error("套餐不存在");
        }
        
        // 更新状态
        setmeal.setStatus(status);
        setmealService.updateById(setmeal);
        return Result.success("更新套餐状态成功");
    }

    /**
     * 根据状态查询套餐
     */
    @GetMapping("/status/{status}")
    public Result getByStatus(@PathVariable Integer status) {
        // 使用传统方式查询，避免Lambda表达式
        List<Setmeal> list = setmealService.list();
        list = list.stream()
                .filter(setmeal -> setmeal.getStatus().equals(status))
                .collect(java.util.stream.Collectors.toList());
        return Result.success(list);
    }
}
